/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       dialog_beastmaster.cpp

	$Header: /game/dialog_beastmaster.cpp $

	$NoKeywords: $

 ************************************************************************/
// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "dialog_beastmaster.h"

#include "bitmap_group.h"
#include "bitmap_group_cache.h"
#include "bitmap_layer_window.h"
#include "bound_handler.h"
#include "button.h"
#include "button_cache.h"
#include "creature_select_window.h"
#include "external_string.h"
#include "game_replace_keywords.h"
#include "hero.h"
#include "interface_help.h"
#include "skill_icon.h"
#include "skill_properties.h"
#include "standard_fonts.h"
#include "stationary_adventure_object.h"
#include "text_window.h"

// ------------------------------------------------------------------
// beastmaster's hut dialog
// ------------------------------------------------------------------
extern t_button_cache const			k_learn_button;

static t_external_string const		k_text_already_known( "already_known.beastmaster" );
static t_external_string const		k_text_brain_full( "brain_full.beastmaster" );
static t_bitmap_group_cache const	k_bitmaps( "dialog.beastmaster" );
t_button_cache const				k_learn_button( "button.learn" );
extern t_bitmap_group_cache const   k_skill_frames;
extern t_button_cache const         k_close_button( "button.close" );
extern t_button_cache			    g_ok_button;

t_dialog_beastmaster::t_dialog_beastmaster( std::vector<t_hero*> const& heroes, 
										    t_skill_type skill, std::string const& intro_text,
										    t_window* parent, t_stationary_adventure_object const& parent_object )
					: t_window( k_completely_transparent, parent ), m_heroes( heroes )
{
	t_screen_rect           rect;
	t_bitmap_layer const*   layer;
	t_window*               window;
	t_window*               background;
	t_screen_point          origin(0,0);
	t_screen_point          offset(0,0);
	t_bitmap_group_ptr      skill_frames;
	t_help_block const&	    shared_help = get_help_block( "shared" );

	m_bitmaps = k_bitmaps.get();
	skill_frames = k_skill_frames.get();
	m_skill = skill;

	// create background
	layer = m_bitmaps->find( "top" );
	window = new t_bitmap_layer_window( layer, origin, this );
	layer = m_bitmaps->find( "left" );
	window = new t_bitmap_layer_window( layer, origin, this );
	layer = m_bitmaps->find( "bottom" );
	window = new t_bitmap_layer_window( layer, origin, this );
	layer = m_bitmaps->find( "right" );
	window = new t_bitmap_layer_window( layer, origin, this );

	layer = m_bitmaps->find( "background" );
	background = new t_bitmap_layer_window( layer, origin, this );
	origin = -layer->get_rect().top_left();

	// create the text
	t_text_window* text_window;
	std::string    text;

    text = parent_object.get_text ("name");
	rect = m_bitmaps->find( "title" )->get_rect() + origin;
	text_window = new t_text_window( get_font( rect.height() ), rect, background, text, t_pixel_24(0,0,0));
	text_window->set_drop_shadow( false );
	text_window->set_center_horizontal();
		     
	text = replace_keywords( intro_text, skill, k_mastery_basic );
	rect = m_bitmaps->find( "text_dialog" )->get_rect() + origin;
	text_window = new t_text_window( get_font( 20 ), rect, background, text, t_pixel_24(0,0,0));
	text_window->set_drop_shadow( false );
		     
	// create the skill icon
	rect = m_bitmaps->find( "skill_icon" )->get_rect() + origin;
	
    layer = skill_frames->find( "released" );
    offset = layer->get_rect().top_left();

	window = new t_bitmap_layer_window( layer, rect.top_left() - offset, background );
    offset = skill_frames->find( "icon" )->get_rect().top_left();

	window = new t_skill_icon( t_skill( skill, k_mastery_basic ), rect + offset, background );
	rect = m_bitmaps->find( "skill_text" )->get_rect() + origin;
	text_window = new t_text_window( get_font( rect.height() / 2 ), rect, background,
		                             get_name( t_skill( skill, k_mastery_basic )),
									 t_pixel_24(0,0,0));
	text_window->set_center_horizontal();
	text_window->set_center_vertical();
	text_window->set_drop_shadow( false );

	// create hero selection control
	t_screen_point point;
	int            i;

	point = m_bitmaps->find("hero_portraits")->get_rect().top_left() + origin;
	m_selection_window = new t_creature_select_window( point, false, background );
	for (i = 0; i < heroes.size(); i++)
	{
		m_selection_window->add( heroes[i] );
		m_selection_window->set_help_text( i, heroes[i]->get_name() );
		if (heroes[i]->get_skill( skill ) >= k_mastery_basic)
		{
			m_selection_window->enable( i, false );
			text = replace_keywords( k_text_already_known, heroes[i] );
			text = replace_keywords( text, t_skill( skill, k_mastery_basic ));
			m_selection_window->set_help_text( i, text );
		}
		else if (heroes[i]->get_primary_skill_count() >= k_primary_skill_limit)
		{
			m_selection_window->enable( i, false );
			text = replace_keywords( k_text_brain_full, heroes[i] );
			m_selection_window->set_help_text( i, text );
		}
	}
	m_selection_window->select_all();
	m_selection_window->set_handler( bound_handler( *this, &t_dialog_beastmaster::selection_change ));

	// create buttons
	t_button*        button;
	t_button_handler handler;

	point = m_bitmaps->find( "learn_button" )->get_rect().top_left() + origin;
	m_learn_button = new t_button( k_learn_button.get(), point, background );
	handler = bound_handler( *this, &t_dialog_beastmaster::learn_clicked );
	m_learn_button->set_click_handler( handler );
    set_help( m_learn_button, shared_help, "learn" );

	point = m_bitmaps->find( "close_button" )->get_rect().top_left() + origin;
	button = new t_button( g_ok_button.get(), point, background );
	handler = bound_handler( *this, &t_dialog_beastmaster::close_clicked );
	button->set_click_handler( handler );
	set_help( button, shared_help, "ok" );

	t_screen_rect parent_rect = get_parent()->get_client_rect();

	rect = m_bitmaps->get_rect();
	rect += (parent_rect.size() - rect.size()) / 2;
	init( rect );
}

// ------------------------------------------------------------------
// beastmaster's hut dialog
// ------------------------------------------------------------------
void t_dialog_beastmaster::close_clicked( t_button* button )
{
	m_return_value = false;
	close();
}

// ------------------------------------------------------------------
// beastmaster's hut dialog
// ------------------------------------------------------------------
void t_dialog_beastmaster::learn_clicked( t_button* button )
{
	int               i;
	t_hero*           hero;

	m_return_value = true;
	for (i = 0; i < m_selection_window->get_count(); i++)
	{
		if (!m_selection_window->is_selected(i))
			continue;
		hero = m_selection_window->get_creature(i)->get_hero();
	    m_learned_heroes.push_back( hero );
		hero->learn_skill( t_skill( m_skill, k_mastery_basic ) );
	}
	m_return_value = true;
	close();
}

// ------------------------------------------------------------------
// beastmaster's hut dialog
// ------------------------------------------------------------------
void t_dialog_beastmaster::selection_change( t_creature_select_window*, t_creature_stack* )
{
	int i;

	for (i = 0; i < m_selection_window->get_count(); i++)
		if (m_selection_window->is_selected(i))
			break;
	m_learn_button->enable( i < m_selection_window->get_count() );
}
